package com.hetang.service.impl;

import com.hetang.common.DateUtils;
import com.hetang.common.StringUtils;
import com.hetang.dao.UserDao;
import com.hetang.domain.User;
import com.hetang.domain.UserRequestDTO;
import com.hetang.domain.common.MsgResponse;
import com.hetang.domain.enums.ResponseCodeEnum;
import com.hetang.domain.enums.UserEnum;
import com.hetang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 詹泽
 * @date 2017/3/3
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Override
    public MsgResponse<User> getUser(int id) {
        User user = userDao.getUser(id);
        if(user  == null){
            return new MsgResponse<>(ResponseCodeEnum.EXCEPTION);
        }
        return new MsgResponse<>(ResponseCodeEnum.SUCCESS,user);
    }

    @Override
    public MsgResponse<List<User>> listUser(UserRequestDTO userRequestDTO) {
        if(userRequestDTO.getUserType().equals("-1")){
            userRequestDTO.setUserType(null);
        }
        String time = userRequestDTO.getTime();
        if(!StringUtils.isNullOrEmpty(time)){
            String[] strs = time.trim().split("-");
            userRequestDTO.setStartTime(DateUtils.parseDate(strs[0],"MM/dd/yyyy"));
            userRequestDTO.setEndTime(DateUtils.parseDate(strs[1],"MM/dd/yyyy"));
        }
        List<User> list = userDao.listUser(userRequestDTO);
        list.forEach(i -> {
            i.setGmtCreateStr(DateUtils.formatDate(i.getGmtCreate(),DateUtils.DAYE_FORMAT_MID));
            i.setGmtLastSigninStr(DateUtils.formatDate(i.getGmtLastSignin(),DateUtils.DAYE_FORMAT_MID));
            if(i.getStatus() == UserEnum.USER_NOT_SEALED.getCode()){
                i.setStatusStr(UserEnum.USER_NOT_SEALED.getValue());
            }else{
                i.setStatusStr(UserEnum.USER_SEALED.getValue());
            }
        });
        int totalCount = userDao.getTotalRecodes(userRequestDTO);
        return new MsgResponse<>(ResponseCodeEnum.SUCCESS,list,totalCount);
    }

    @Override
    public MsgResponse<Object> updateStatus(String id, int status) {
        boolean flag = false;
        if(status == 1){
            flag = userDao.updateUserStatus(id, UserEnum.USER_SEALED.getCode());
        }else if(status == 2){
            flag = userDao.updateUserStatus(id,UserEnum.USER_NOT_SEALED.getCode());
        }
        if(flag){
            return new MsgResponse<>(ResponseCodeEnum.SUCCESS);
        }
        return new MsgResponse<>(ResponseCodeEnum.EXCEPTION);
    }

    @Override
    public MsgResponse<Object> updateUser(User user) {
        if(user.getId() == null){
            return new MsgResponse<>(ResponseCodeEnum.REQUEST_PARAMETER_ERROR);
        }
        if(userDao.updateUser(user)){
            return new MsgResponse<>(ResponseCodeEnum.SUCCESS);
        }
        return new MsgResponse<>(ResponseCodeEnum.EXCEPTION);
    }

    @Override
    public MsgResponse<Object> deleteUser(String id) {
        if(userDao.deleteUser(id)){
            return new MsgResponse<>(ResponseCodeEnum.SUCCESS);
        }
        return new MsgResponse<>(ResponseCodeEnum.EXCEPTION);
    }

    @Override
    public MsgResponse<Object> addUser(User user) {
        if(StringUtils.isNullOrEmpty(new String[]{user.getCode(),user.getName(),user.getNotes(),user.getUserType()})){
            return new MsgResponse<>(ResponseCodeEnum.REQUEST_PARAMETER_ERROR);
        }
        if(user.getStatus() == null){
            return new MsgResponse<>(ResponseCodeEnum.REQUEST_PARAMETER_ERROR);
        }
        user.setGmtCreate(new Date());
        user.setGmtLastSignin(new Date());
        if(userDao.addUser(user)){
            return new MsgResponse<>(ResponseCodeEnum.SUCCESS);
        }
        return new MsgResponse<>(ResponseCodeEnum.EXCEPTION);
    }
}
